//
//  MyStatusOriginalView.swift
//  weibo08
//
//  Created by 陈杰明 on 16/3/6.
//  Copyright © 2016年 JM. All rights reserved.
//

import UIKit
import SnapKit

let MyStatusCellMargin: CGFloat = 8.0

class MyStatusOriginalView: UIView {
    //属性
    //底部约束
    var statusOriginalBottom: Constraint?
    var status: MyStatus? {
        didSet{
            //当cell给MyStatusOriginalView设置模型时，给控件设置内容
            //头像
            if let imageStr = status?.user?.profile_image_url {
                let url = NSURL(string: imageStr)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            //名字
            nameLabel.text = status?.user?.name
            //会员等级
            memberView.image = status?.user?.mbImage
            //时间
            timeLabel.text = status?.created_at
            //来源
            sourceLabel.text = status?.source
            //微博认证
            verifiedTypeImage.image = status?.user?.verifiedTypeImage
            //微博内容
            contentLabel.text = status?.text
            
            //删除之前的约束
            statusOriginalBottom?.uninstall()
            //判断是否有配图
            if let picture = status?.pic_urls?.count where picture > 0 {
                pictureView.hidden = false
                //设置配图的status
                pictureView.status = status
                self.snp_makeConstraints { (make) -> Void in
                    self.statusOriginalBottom = make.bottom.equalTo(pictureView).offset(MyStatusCellMargin).constraint
                }
            }else {
                pictureView.hidden = true
                self.snp_makeConstraints { (make) -> Void in
                    self.statusOriginalBottom = make.bottom.equalTo(contentLabel).offset(MyStatusCellMargin).constraint
                }
            }
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.backgroundColor = UIColor.whiteColor()
        
        prepareUI()
    }
    
    func prepareUI() {
        //添加子控件
        self.addSubview(iconView)
        self.addSubview(nameLabel)
        self.addSubview(memberView)
        self.addSubview(timeLabel)
        self.addSubview(sourceLabel)
        self.addSubview(verifiedTypeImage)
        self.addSubview(contentLabel)
        self.addSubview(pictureView)
        
        //添加约束
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(MyStatusCellMargin)
            make.width.height.equalTo(35)
        }
        //名字
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(MyStatusCellMargin)
        }
        //会员
        memberView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(MyStatusCellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(MyStatusCellMargin)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(MyStatusCellMargin)
        }
        //认证
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(iconView.snp_trailing)
            make.centerX.equalTo(iconView.snp_bottom)
            //            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        //微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(MyStatusCellMargin)
            make.trailing.equalTo(self).offset(-MyStatusCellMargin)
            
        }
        //配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(MyStatusCellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(MyStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        //设置MyStatusOriginalView和contentLabel的bottom重合
        self.snp_makeConstraints { (make) -> Void in
            
            self.statusOriginalBottom = make.bottom.equalTo(pictureView).offset(MyStatusCellMargin).constraint
        }
        
    }
    //MARK: - 懒加载
    // 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    // 名字
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), font: 14)
    // 会员等级
    private lazy var memberView: UIImageView = UIImageView(image: nil)
    // 时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), font: 11)
    // 来源
    private lazy var sourceLabel: UILabel =  UILabel(textColor: UIColor.lightGrayColor(), font: 11)
    // 认证类型
    private lazy var verifiedTypeImage: UIImageView = UIImageView(image: nil)
    // 微博内容
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel(textColor: UIColor.darkGrayColor(), font: 15)
        
        label.numberOfLines = 0
        
        return label
    }()
    //配图
    private lazy var pictureView: MyStatusPictureView = MyStatusPictureView()
}
